<template>
  <div class="rule-body">
    <div class="rule-header mb10" v-hasPermi="['system:overtime_rules:add']">
      <el-button type="primary" icon="el-icon-plus" size="small" @click="handleUpdate()">新增加班规则</el-button>
    </div>
    <el-table
      v-loading="loading"
      :data="ruleList"
    >
      <el-table-column label="规则名称" align="center" prop="name" width="200">
        <template slot-scope="scope">
          <el-radio v-model="ruleCheckId" :label="scope.row.id" name=" ">
            <span class="ruleName">
              {{ scope.row.name }}
            </span>
          </el-radio>
        </template>
      </el-table-column>
      <el-table-column label="规则内容" align="center" prop="contacts">
        <template slot-scope="scope">
          <p class="rule-p" v-if="scope.row.overtimeWork.type">
            工作日:
            {{
              scope.row.overtimeWork.method == 1 ? '加班必须审批，加班时长以审批单为准' : (scope.row.overtimeWork.method == 2 ? '加班必须审批，以打卡为准，但不能超过审批单时长' : '加班无须审批，根据打卡时间计算加班时长')
            }}
          </p>
          <p class="rule-p" v-else>
            工作日: 不允许加班
          </p>
          <p class="rule-p" v-if="scope.row.overtimeRest.type">
            休息日:
            {{
              scope.row.overtimeRest.method == 1 ? '加班必须审批，加班时长以审批单为准' : (scope.row.overtimeRest.method == 2 ? '加班必须审批，以打卡为准，但不能超过审批单时长' : '加班无须审批，根据打卡时间计算加班时长')
            }}
          </p>
          <p class="rule-p" v-else>
            休息日: 不允许加班
          </p>
          <p class="rule-p" v-if="scope.row.overtimeHoliday.type">
            节假日:
            {{
              scope.row.overtimeHoliday.method == 1 ? '加班必须审批，加班时长以审批单为准' : (scope.row.overtimeHoliday.method == 2 ? '加班必须审批，以打卡为准，但不能超过审批单时长' : '加班无须审批，根据打卡时间计算加班时长')
            }}
          </p>
          <p class="rule-p" v-else>
            节假日: 不允许加班
          </p>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="90"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >编辑
          </el-button
          >
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:post:edit']"-->
<!--          >删除-->
<!--          </el-button-->
<!--          >-->
        </template>
      </el-table-column>
    </el-table>

<!--    <pagination-->
<!--      v-show="total > 0"-->
<!--      :total="total"-->
<!--      :page.sync="queryParams.pageNum"-->
<!--      :limit.sync="queryParams.pageSize"-->
<!--      layout='total, prev, pager, next'-->
<!--      @pagination="getList"-->
<!--    />-->
  </div>
</template>

<script>
import {listOvertimeRules} from '@/api/system/rulesOverTime.js'
import {delOvertimeRules} from '@/api/system/rulesOverTime.js'

export default {
  props: ['config'],
  name: "rule-table",
  data() {
    return {
      showConfig: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined
      },
      total: 0,
      ruleList: [],
      loading: false,
      ruleCheckId: 0
    }
  },
  mounted() {
    console.log('收到数据：', this.config)
    if (this.config) {
      this.ruleCheckId = this.config.id
    }
  },
  methods: {
    getList() {
      listOvertimeRules(this.queryParams).then(res => {
        console.log('res===>', res)
        this.ruleList = res.rows
        this.total = res.total
      })
    },
    handleUpdate(row) {
      this.$emit('ruleEdit', row ? this.$deepCopy(row) : null)
    },
    getCheckFn() {
      if (!this.ruleCheckId) {
        this.$message.error('选择加班规则');
        return
      }
      let row = {}
      for (let item of this.ruleList) {
        if (item.id === this.ruleCheckId) {
          row = this.$deepCopy(item)
        }
      }
      this.$emit('checkRuleGet', {
        type: 'rule',
        data: row
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除"' + postIds + '"的数据项？').then(function () {
        return delOvertimeRules(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped>
.rule-body .el-radio {
  display: flex;
}

.ruleName {
  display: block;
  width: 150px;
  white-space: break-spaces;
  line-height: 1.5;
}

.rule-p {
  text-align: left;
  margin: 0;
  color: #000;
}
</style>
